package org.hxyjs.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import org.hxyjs.aop.SystemMail;
import org.hxyjs.dto.DTO;

import org.hxyjs.entity.MemberType;
import org.hxyjs.page.PageInfo;
import org.hxyjs.service.MemberTypeService;
import org.hxyjs.vo.MemberTypeView;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;


@CrossOrigin
@Controller //@Controller注解是用spring容器生成 对象 默认对象名 memberTypeController
@RestController //@RequestMapping 用户请求查找该资源使用的名字 网名 默认  /memberTypeController
public class MemberTypeController {
	@Resource
	MemberTypeService memberTypeService;
	@RequestMapping("/memberTypes")
	@ResponseBody //把方法的返回值按json转换之后 输出到浏览器
	public DTO<MemberType> getAllMemberTypes() throws Exception{
		System.out.println("查询全部");
		return memberTypeService.getAllMemberTypes();
	}

	@RequestMapping("/memberTypes/{mtno}")
	@ResponseBody //把方法的返回值按json转换之后 输出到浏览器
	public DTO<MemberType> getMemberTypeByUno(@PathVariable("mtno") String mtno) throws Exception{
		System.out.println(mtno + "查询成功");
		return memberTypeService.getMemberTypeByUno(mtno);
	}
	//@SystemMail(op = "MemberType:addMemberType",queueName = "family.mail",module = "用户管理",methods = "用户注册")
	@PostMapping("/memberTypes")
	@ResponseBody
	public DTO<MemberType> addMemberTypeByUno(@RequestBody MemberType memberType) throws Exception{//如何从页面传入参数到方法 用这个注解绑定变量@PathVariable
		System.out.println("增加成功");
		return memberTypeService.addMemberTypeByUno(memberType);
	}

	@DeleteMapping("/memberTypes/{mtno}")
	@ResponseBody
	public DTO<MemberType> deleteMemberType(@PathVariable("mtno") String mtno) throws Exception{//如何从页面传入参数到方法 用这个注解绑定变量@PathVariable
		System.out.println(mtno+"删除成功");
		return memberTypeService.deleteMemberType(mtno);
	}

	@RequestMapping("/getMemberTypesWithOrders/{mtno}")
	@ResponseBody
	public DTO<MemberType> getMemberTypesWithOrders(@PathVariable("mtno") String mtno,HttpServletRequest request,HttpServletResponse response) throws Exception{//如何从页面传入参数到方法 用这个注解绑定变量@PathVariable
		System.out.println("查询学生选课111");
		DTO<MemberType> dto = memberTypeService.getMemberTypesWithOrders(mtno);
		saveSelectCourseCookieProcess(dto.getT(),request,response);
		return  dto;
	}
	@PatchMapping("/memberTypes")
	@ResponseBody
	public DTO<MemberType> updateMemberType(@RequestBody MemberTypeView memberTypeview) throws Exception{//如何从页面传入参数到方法 用这个注解绑定变量@PathVariable
		System.out.println("修改成功");
		System.out.println(memberTypeview);
		return memberTypeService.updateMemberType(memberTypeview);
	}
	@RequestMapping("/memberTypes/pageInfo")
	@ResponseBody
	public DTO<MemberType> getMemberTypesByPage(PageInfo pageInfo) throws Exception {//如何从页面传入参数到方法 用这个注解绑定变量@PathVariable
		System.out.println("查询分页");
		return memberTypeService.getMemberTypesByPage(pageInfo);
	}
	public DTO<MemberType> saveSelectCourseCookieProcess(MemberType memberType, HttpServletRequest request, HttpServletResponse response) throws JsonProcessingException, UnsupportedEncodingException {
		//cookie是以key value形式存放在浏览器的文本
		DTO dto = new DTO(200,"cookie保存成功");
//		if(memberType!=null&&memberType.getUno()!=null) {
//			ObjectMapper objectMapper = new ObjectMapper();
//			Optional<List<OrderForm>> orderFormsOp = Optional.ofNullable(memberType.getOrderForms());
//			List<OrderForm> orderForms = null;
//			if (orderFormsOp.isPresent()&&orderFormsOp.get().size()>0) {
//				orderForms = new ArrayList<OrderForm>(memberType.getOrderForms());
//
//				for (OrderForm orderForm : memberType.getOrderForms()) {
//					Cookie orderFormsCookie = new Cookie("memberType" + memberType.getUno() + "_" + orderForm.getUrno(), URLEncoder.encode(objectMapper.writeValueAsString(orderForm), "UTF-8"));
//
//					//memberTypeCookie.setMaxAge(30*24*60*60);   //存活期为一个月 30*24*60*60
//					orderFormsCookie.setPath("/");
//					orderFormsCookie.setDomain("localhost");
//					orderFormsCookie.setMaxAge(2 * 24 * 60 * 60);   //存活期为2天 2*24*60*60
//					response.addCookie(orderFormsCookie);
//
//				}
//			}
//			memberType.setOrderForms(null);
//			Cookie memberTypeCookie = new Cookie("memberType" + memberType.getUno(), URLEncoder.encode(objectMapper.writeValueAsString(memberType), "UTF-8"));
//			memberTypeCookie.setPath("/");
//			memberTypeCookie.setDomain("localhost");
//			memberTypeCookie.setMaxAge(2 * 24 * 60 * 60);   //存活期为2天 2*24*60*60
//			response.addCookie(memberTypeCookie);
//			memberType.setOrderForms(orderForms);
//		}else{
//			dto.setCode(406);
//			dto.setMsg("缺少必要的参数，非法操作");
//		}
		return dto;
	}



	@RequestMapping("/activate/{mtno}")
	@ResponseBody
	public DTO<MemberType> activate(@PathVariable("mtno") String mtno) throws Exception{

		return memberTypeService.activate(mtno);
	}

	@SystemMail(op = "MemberType:lettory",queueName = "family.mail",module = "中奖",methods = "中奖信息")
	@RequestMapping("/lettory/{mtno}/{giftName}")
	@ResponseBody
	public DTO<MemberType> lettory(@PathVariable("mtno") String mtno,@PathVariable("giftName")String giftName) throws Exception{

		return memberTypeService.lettory(mtno,giftName);
	}



}
